﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;

/// <summary>
/// JoinActivity 的摘要说明
/// </summary>
public class JoinActivity
{
    public JoinActivity()
    {
    }

    public int JoinActivitys(int ActivityID, int MemID, Chain.Model.SysUser UserModel)
    {
        int flag = 0;

        //1.判断该会员是否参加过此活动
        Chain.BLL.Mem_Activity maBll = new Chain.BLL.Mem_Activity();
        Chain.Model.Mem_Activity maModel = new Chain.Model.Mem_Activity();

        if (maBll.Exists(MemID, ActivityID))
        {

            flag = -13;
        }
        else
        {
            //2.参加活动，插入会员——奖励关系表
            Chain.BLL.Activity activity = new Chain.BLL.Activity();
            bool iscovered = activity.IsCovered(ActivityID);

            Chain.BLL.Activity_Gifts awards = new Chain.BLL.Activity_Gifts();
            DataTable dt = awards.GetAwardsInfo(ActivityID);

            List<string> typelist = new List<string>();
            foreach (DataRow dr in dt.Rows)
            {
                typelist.Add(dr["type"].ToString());
            }

            Dictionary<string, Type> dic = new Dictionary<string, Type>();

            dic.Add("积分", typeof(Score));
            dic.Add("储值", typeof(Money));
            dic.Add("推广奖励", typeof(Extend));
            dic.Add("返利", typeof(Rebate));
            dic.Add("分享奖励", typeof(Share));
            dic.Add("小礼品", typeof(Presents));
            dic.Add("代金券", typeof(Ticket));

            Gifts decorator = new Base();
            decorator.MemID = MemID;
            decorator.ActivityID = ActivityID;
            decorator.UserModel = UserModel;
            decorator.IsCovered = iscovered;

            foreach (string item in typelist)
            {
                Type o = dic[item];
                decorator = Activator.CreateInstance(o, decorator) as Decorator;

            }
            flag = decorator.Create();

            //3.插入会员活动记录表
            maModel.MemID = MemID;
            maModel.ActivityID = ActivityID;
            maModel.moeny = 0;
            maModel.score = 0;
            maModel.CouponTitle = "";
            maModel.PresentsName = "";
            maModel.Mem_ShareId = 0;
            maModel.Mem_ExtendId = 0;
            maModel.Mem_RebateId = 0;
         
            foreach (DataRow dr in dt.Rows)
            {
                switch (dr["type"].ToString().Trim())
                {
                    case "积分":
                        maModel.score = Convert.ToDecimal(dr["value"].ToString());
                        break;
                    case "储值":
                        maModel.moeny = Convert.ToDecimal(dr["value"].ToString());
                        break;
                    case "代金券":
                        maModel.CouponTitle = dr["value"].ToString();
                        break;
                    case "小礼品":
                        maModel.PresentsName = dr["value"].ToString();
                        break;
                    case "分享奖励":
                        maModel.Mem_ShareId = GetMem_AwardsID("分享奖励",MemID);
                        break;
                    case "推广奖励":
                        maModel.Mem_ExtendId = GetMem_AwardsID("推广奖励", MemID);
                        break;
                    case "返利":
                        maModel.Mem_RebateId = GetMem_AwardsID("返利", MemID);
                        break;
                }
            }
            flag = maBll.Add(maModel);

        }
        return flag;
    }



    public int GetMem_AwardsID(string type, int MemID)
    {
        int ID = 0;

        switch (type)
        {
            case "分享奖励":
                Chain.BLL.Mem_Share msBll = new Chain.BLL.Mem_Share();
                if (msBll.IsExist(MemID)) {
                    DataTable dt = msBll.GetList(" MemID=" + MemID + " and isonline=1 and isdel=0  and EndTime>getdate() and StartTime<GETDATE()").Tables[0];
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        ID = Convert.ToInt32(msBll.GetList(" MemID=" + MemID + " and isonline=1 and isdel=0  and EndTime>getdate() and StartTime<GETDATE()").Tables[0].Rows[0]["ID"].ToString());
                    }
                    }            
                break;
            case "推广奖励":
                Chain.BLL.Mem_Extend meBll = new Chain.BLL.Mem_Extend();
                if (meBll.IsExist(MemID))
                {
                    DataTable dt = meBll.GetList(MemID);
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        ID = Convert.ToInt32(meBll.GetList(MemID).Rows[0]["ID"].ToString());
                    }
                }
                break;
            case "返利":
                Chain.BLL.Mem_Rebate mrBll = new Chain.BLL.Mem_Rebate();
                if (mrBll.IsExist(MemID))
                {
                    Chain.Model.Mem_Rebate newmbBll = mrBll.GetModel(" MemID=" + MemID + " and isonline=1 and isdel=0  and EndTime>getdate() and StartTime<GETDATE()");
                    if (newmbBll!=null)
                    {
                        ID = Convert.ToInt32(mrBll.GetModel(" MemID=" + MemID + " and isonline=1 and isdel=0  and EndTime>getdate() and StartTime<GETDATE()").ID);

                    }
                }
                break;
        }
        return ID;
    }

}